package com.game.server.system;

import com.comm.ISession;
import com.game.majiang.config.LogManager;
import com.game.server.message.IRespose;

/**
 * @author 
 */
public abstract class AbstractClient {

    private ISession session;

    protected String ip;

    /*** �û�ID */
    protected int userId;

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }
    
    public String getIp() {
    	if(ip != null && userId>10000 && !"".equals(ip)){
    		String IP=ip.replace("/", "");
        	String[]  strs=IP.split(":");
            return strs[0].toString();
    	}
    	 return "";
    }

    protected long getSessionid() {
        if (session == null) return -1;
        return session.getId();
    }

    public ISession getSession() {
        return session;
    }
    
    public void setSession(ISession session) {
    	//this.session.close();
        this.session = session;
    }

    public AbstractClient(int uid, ISession session) {
        this.userId = uid;
        if(session != null)
        {
        	this.session = session;
        	ip = session.getIP();
        }
        else
        {
        	ip = "0.0.0.0";
        }
    }

    public AbstractClient(int uid) {
        this.userId = uid;
	}
    public AbstractClient() {
	}
	public void Close()
    {
		  if(session != null)
    	  {
			  session.close();
    	  }
    }
    
    public boolean Send(IRespose rsp){
    	  if(session != null)
    	  {
    		  if(session.isConnected())
    		  {
//      	    	log.info("send:"+rsp.GetMsgCode() +" "+rsp.getClass().toString());
    			  LogManager.getInstance().Log("send:"+rsp.GetMsgCode() +" "+rsp.getClass().toString());
    		    session.send(rsp.Serialize());
    		    return true;
    		  }
    		  else
    		  {
    			return false;
    		  }
    	  }
    	  else
    		  return false;
    }
}
